package com.lyn.plugin.proxy;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.lyn.plugin.entity.Plugin;

/**
 * 插件
 */
public abstract class BasePlugin implements PluginInterface {
    public Plugin plugin;

    @Override
    public void onPlugin(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public Object onLifeCycle(String methodName, Object... objects) {
        if ("onCreate".equals(methodName)) {
            onCreate((Bundle) objects[0]);
            return true;
        }
        if ("onStart".equals(methodName)) {
            onStart();
            return true;
        }
        if ("onRestart".equals(methodName)) {
            onRestart();
            return true;
        }

        if ("onResume".equals(methodName)) {
            onResume();
            return true;
        }
        if ("onPause".equals(methodName)) {
            onPause();
            return true;
        }
        if ("onStop".equals(methodName)) {
            onStop();
            return true;
        }
        if ("onDestroy".equals(methodName)) {
            onDestroy();
            return true;
        }
        return false;
    }

    public void onCreate(Bundle savedInstanceState) {

    }

    public void onStart() {

    }

    public void onRestart() {

    }

    public void onResume() {

    }

    public void onPause() {

    }

    public void onStop() {

    }

    public void onDestroy() {

    }

    public void setContentView(int layoutId) {
        plugin.activity.setContentView(getView(layoutId));
    }

    public LayoutInflater getLayoutInflater() {
        return plugin.activity.getLayoutInflater();
    }

    public View getView(int layoutId) {
        XmlResourceParser xmlResourceParser = plugin.resources.getLayout(layoutId);
        return getLayoutInflater().inflate(xmlResourceParser, null, false);
    }

    public <T extends View> T findViewById(int id) {
        return plugin.activity.findViewById(id);
    }

    public Resources getResources() {
        return plugin.resources;
    }

    public Context getContext() {
        return plugin.activity;
    }

    public String getString(String name) {
        int identifier = plugin.resources.getIdentifier(name, "string", plugin.packageName);
        return getString(identifier);
    }

    public Drawable getDrawable(String name) {
        int identifier = plugin.resources.getIdentifier(name, "string", plugin.packageName);
        return getDrawable(identifier);
    }


    public String getString(int id) {
        return plugin.resources.getString(id);
    }

    public Drawable getDrawable(int id) {
        return plugin.resources.getDrawable(id);
    }
}
